package org.theonept.travian.model;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.theonept.travian.util.HttpUtil;

public class Production extends Entity {
	List<Ressource> ressources;

	private Integer prodBois;
	private Integer prodFer;
	private Integer prodArgile;
	private Integer prodCereale;

	private Integer stockBois;
	private Integer stockFer;
	private Integer stockArgile;
	private Integer stockCereale;
	private Integer stockMaxRessources;
	private Integer stockMaxCereale;
	private Village village;

	public List<Ressource> getRessources() {
		ressources = Ressource.getRessources(this);

		return ressources;
	}

	public void setRessources(List<Ressource> ressources) {
		this.ressources = ressources;
	}

	public Integer getStockBois() {
		return stockBois;
	}

	public void setStockBois(Integer stockBois) {
		this.stockBois = stockBois;
	}

	public Integer getStockFer() {
		return stockFer;
	}

	public void setStockFer(Integer stockFer) {
		this.stockFer = stockFer;
	}

	public Integer getStockArgile() {
		return stockArgile;
	}

	public void setStockArgile(Integer stockArgile) {
		this.stockArgile = stockArgile;
	}

	public Integer getStockCereale() {
		return stockCereale;
	}

	public void setStockCereale(Integer stockCereale) {
		this.stockCereale = stockCereale;
	}

	public Integer getStockMaxCereale() {
		return stockMaxCereale;
	}

	public void setStockMaxCereale(Integer stockMaxCereale) {
		this.stockMaxCereale = stockMaxCereale;
	}

	public Integer getProdBois() {
		return prodBois;
	}

	public void setProdBois(Integer prodBois) {
		this.prodBois = prodBois;
	}

	public Integer getProdFer() {
		return prodFer;
	}

	public void setProdFer(Integer prodFer) {
		this.prodFer = prodFer;
	}

	public Integer getProdArgile() {
		return prodArgile;
	}

	public void setProdArgile(Integer prodArgile) {
		this.prodArgile = prodArgile;
	}

	public Integer getProdCereale() {
		return prodCereale;
	}

	public void setProdCereale(Integer prodCereale) {
		this.prodCereale = prodCereale;
	}

	/**
	 * @return the stockMaxRessources
	 */
	public Integer getStockMaxRessources() {
		return stockMaxRessources;
	}

	/**
	 * @param stockMaxRessources
	 *            the stockMaxRessources to set
	 */
	public void setStockMaxRessources(Integer stockMaxRessources) {
		this.stockMaxRessources = stockMaxRessources;
	}

	public static Production getProduction(Village village) {
		Production production = new Production();
		Document documentProduction = HttpUtil.getDocumentFromUrl(village
				.getCompte().getUrlServeur()
				+ "dorf1.php?newdid="
				+ village.getIdVillage());
		production.setStockMaxRessources(Integer.valueOf(documentProduction
				.getElementById("stockBarWarehouse").text().replace(" ", "")));

		production.setStockBois(Integer.valueOf(documentProduction
				.getElementById("l1").text().replace(" ", "")));

		production.setStockArgile(Integer.valueOf(documentProduction
				.getElementById("l2").text().replace(" ", "")));

		production.setStockFer(Integer.valueOf(documentProduction
				.getElementById("l3").text().replace(" ", "")));
		production.setStockMaxCereale(Integer.valueOf(documentProduction
				.getElementById("stockBarGranary").text().replace(" ", "")));
		production.setStockCereale(Integer.valueOf(documentProduction
				.getElementById("l4").text().replace(" ", "")));

		List<Element> elementProduction = documentProduction.getElementById(
				"production").getElementsByAttributeValue("class", "num");
		for (int i = 0; i < elementProduction.size(); i++) {
			if (i == 0) {
				production.setProdBois(getInt(elementProduction.get(i).text()
						.replace(" ", "")));
			} else if (i == 1) {
				production.setProdArgile(getInt(elementProduction.get(i).text()
						.replace(" ", "")));
			} else if (i == 2) {
				production.setProdFer(getInt(elementProduction.get(i).text()
						.replace(" ", "")));
			} else if (i == 3) {
				production.setProdCereale(getInt(elementProduction.get(i)
						.text().replace(" ", "")));
			}
			elementProduction.get(i).text();
		}
		production.setVillage(village);
		return production;

	}

	/**
	 * @return the village
	 */
	public Village getVillage() {
		return village;
	}

	/**
	 * @param village
	 *            the village to set
	 */
	public void setVillage(Village village) {
		this.village = village;
	}

	public static int getInt(String number) {
		String valeur = "";
		for (int i = 0; i < number.toCharArray().length; i++) {
			Character character = number.toCharArray()[i];

			if (StringUtils.isNumeric(character.toString())) {
				valeur = valeur + character.toString();
			}
		}
		return Long.valueOf(NumberUtils.toLong(valeur, 0L)).intValue();
	}
}
